#include "OpenglAppFramework/inc/OpenglAppFramework.h"
#include <iostream>
#include <dlfcn.h>

using namespace std;

#define SCREEN_WIDTH    (800)
#define SCREEN_HEIGHT   (600)

void printUsage() {
    cout << "Usage: ./shadow <config_path> <res_dir> [dll_path]" << endl;
}

int main(int argc, const char** argv) {
    if(argc < 3 || argc > 4) {
        printUsage();
        return 1;
    }
    OpenglAppConfig::resource_dir = argv[2];
    OpenglAppConfig::width = SCREEN_WIDTH;
    OpenglAppConfig::height = SCREEN_HEIGHT;

    // std::cout << "starter ..." << std::endl;
    OpenglApp app;
    OpenglEnv& env = app.manager().env();
    if(argc == 4) {
        OpenglAppConfig::dll_path = argv[3];
        env.dll_handler = dlopen(OpenglAppConfig::dll_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
        if(env.dll_handler == nullptr) {
            cout << "load dll failed: " << OpenglAppConfig::dll_path << endl;
            return -1;
        }
    }
    if(app.init("Basic Example", argv[1]) != 0) {
        cout << "OpenglApp init failed." << endl;
    }
    else {
        app.start();
    }
    app.destroy();
    if(env.dll_handler != nullptr) {
        dlclose(env.dll_handler);
    }
    return 0;
}
